/*
 * @FileName: [RechargeableCardReconciliationImpl.java] 
 * @Package com.bcinfo.iccard.service.impl 
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com.
 * All rights reserved.
 * 
 * This software is the confidential and proprietary 
 * information of BCINFO Technology Limited Company
 * ("Confidential Information"). You shall not disclose 
 * such Confidential Information and shall use it only
 * in accordance with the terms of the contract agreement 
 * you entered into with RKY.
 * 
 * $Rev$
 * $LastChangedDate$
 * $LastChangedBy$
 * 
 * @category ICCARD
 * @version 1.1
 * @author $Author$
 *
 * Change History:[Formatter: author date description] <br/>
 * 1
 * 2
 * 3
*/


package com.bcinfo.iccard.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bcinfo.iccard.dao.RechargeableCardCreditDetailMapper;
import com.bcinfo.iccard.exception.IccardDatabaseException;
import com.bcinfo.iccard.exception.InvalidParametersException;
import com.bcinfo.iccard.service.RechargeableCardReconciliationService;
import com.bcinfo.iccard.util.PaginationList;

/**
 * 一句话描述 <br/>
 * <p>Description:<br/>
 *    <p>TODO 详细描述
 *
 * <p>For Examples <br/>
 * <PRE>
 *      TODO 代码使用示例
 * </PRE>
 * <p>
 */
@Service
public class RechargeableCardReconciliationImpl implements RechargeableCardReconciliationService
{
 
    @Autowired
    private RechargeableCardCreditDetailMapper _rechargeableCardCreditDetailMapper ;
    
    private static final Logger _logger = Logger.getLogger(RechargeableCardReconciliationService.class);

    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @return 
     * @see com.bcinfo.iccard.service.RechargeableCardReconciliationService#test()
     */
    @Override
    public PaginationList<Map<String,Object>> getRechargeableCardList(String userId,int pageSize, int pageNo) 
            throws IccardDatabaseException, InvalidParametersException
    {
        PaginationList<Map<String,Object>> paginationList = new PaginationList<Map<String,Object>>();
        List<Map<String,Object>> list = null;
        int totalLogCount = 0;
        if (_logger.isDebugEnabled())
        {
            _logger.info("find:,userId=" + userId + ",pageSize=" + pageSize
                    + ",pageNo=" + pageNo);
        }
        try
        {
            totalLogCount = _rechargeableCardCreditDetailMapper.selectRechargeableCardReconciliationCount(userId);
        }
        catch (Exception e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
        if (pageNo <= 0 || totalLogCount < (pageNo - 1) * pageSize || pageSize <= 0)
        {
            _logger.error("parameter is wrong");
            throw new InvalidParametersException();
        }
        int benginIndex = pageSize * pageNo - pageSize;
        try
        {
            list = _rechargeableCardCreditDetailMapper.selectRechargeableCardReconciliationList(userId, benginIndex, pageSize);
        }
        catch (Exception e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }

        int totalPage = totalLogCount % pageSize == 0 ? totalLogCount / pageSize : totalLogCount / pageSize + 1;
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(list);
        paginationList.setTotalRecord(totalLogCount);
        return paginationList;
    }
    
    /*
     * (non-Javadoc)
     * Description: <br/> 
     *
     * @param userId
     * @param dzsj
     * @param pageSize
     * @param pageNo
     * @return
     * @throws IccardDatabaseException
     * @throws InvalidParametersException 
     * @see com.bcinfo.iccard.service.RechargeableCardReconciliationService#getReconciliationList(java.lang.String, java.lang.String, int, int)
     */
    public PaginationList<Map<String,Object>>  getReconciliationList(String userId,String dzsj,String newDate,String opStatus,int pageSize, int pageNo) throws IccardDatabaseException, InvalidParametersException{
        PaginationList<Map<String,Object>> paginationList = new PaginationList<Map<String,Object>>();
        List<Map<String,Object>> list = null;
        int totalLogCount = 0;
        if (_logger.isDebugEnabled())
        {
            _logger.info("find:,userId=" + userId + ",scdzrq="+dzsj+",pageSize=" + pageSize
                    + ",pageNo=" + pageNo);
        }
        try
        {
            totalLogCount = _rechargeableCardCreditDetailMapper.selectReconciliationCount(userId, dzsj,newDate,opStatus);
        }
        catch (Exception e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
        if (pageNo <= 0 || totalLogCount < (pageNo - 1) * pageSize || pageSize <= 0)
        {
            _logger.error("parameter is wrong");
            throw new InvalidParametersException();
        }
        int benginIndex = pageSize * pageNo - pageSize;
        try
        {
            list = _rechargeableCardCreditDetailMapper.selectReconciliationList(userId,dzsj,newDate,opStatus,benginIndex, pageSize);
        }
        catch (Exception e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }

        int totalPage = totalLogCount % pageSize == 0 ? totalLogCount / pageSize : totalLogCount / pageSize + 1;
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(list);
        paginationList.setTotalRecord(totalLogCount);
        return paginationList;
    }
    
    /*
     * (non-Javadoc)
     * Description: <br/> 
     *
     * @param userId
     * @param dzsj
     * @param newDate
     * @param opStatus
     * @return
     * @throws IccardDatabaseException
     * @throws InvalidParametersException 
     * @see com.bcinfo.iccard.service.RechargeableCardReconciliationService#getReconciliationSum(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
     */
    public String getReconciliationSum(String userId,String dzsj,String newDate,String opStatus) throws IccardDatabaseException, InvalidParametersException{
        String reconciliationSum = "";
        if (_logger.isDebugEnabled())
        {
            _logger.info("find:,userId=" + userId + ",scdzrq="+dzsj);
        }
        try
        {
            reconciliationSum = _rechargeableCardCreditDetailMapper.selectReconciliationSum(userId,dzsj,newDate,opStatus);
        }
        catch (Exception e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
        
        return reconciliationSum;
    }
    
    
    public String checkCountReconciliation(String userId,String scdzsj,String newDate,String opStatus,String account,String dzje) throws IccardDatabaseException, InvalidParametersException{
        String backInfo = "0";
        if (_logger.isDebugEnabled())
        {
            _logger.info("update:,userId=" + userId + ",scdzrq="+scdzsj+",newDate="+newDate+",opStatus="+opStatus+",account="+account+"dzje="+dzje);
        }
        try{
            _rechargeableCardCreditDetailMapper.updateReconciliation(userId, scdzsj, newDate, opStatus);
            _rechargeableCardCreditDetailMapper.insertReconciliationLog(userId, scdzsj, newDate, dzje,account);
        }catch (Exception e)
        {
            backInfo = "1";
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new IccardDatabaseException();
        }
        return backInfo;
    }
}
